/**
 *  world_manager.cpp
 *  AwkwardPaint - Engine
 *
 *  Date: 15/08/2013
 *
 *  Language: C++
 *
 *  @author Carl McCaffrey
 *
 */
#include "world/world_manager.h"

#include <stdio.h>
#include <string>

Level* WorldManager::level;

void WorldManager::LoadLevel(Renderer* renderer, const char* filename)
{
    dbg_output->Print("Loading Level %s...\n", filename);
    level = new Level;
    std::vector<std::string*> contents;
    std::ifstream file;

    file.open(filename);
    if(!file.is_open()){
        dbg_output->Print("Failed to open Level file %s!\n", filename);
        return;
    }

    dbg_output->Print("\tReading File...\n");
    char line[256];
    while(!file.eof()){
        file.getline(line, 256);
        contents.push_back(new std::string(line));
    }

    for(unsigned int i = 0; i < contents.size(); i++){
        if(contents[i]->c_str()[0] == 'f'){
            char field_image[128];
            sscanf(contents[i]->c_str(), "f %s", field_image);
            dbg_output->Print("\tLoading Field: %s\n", field_image);
            renderer->CreateSprite(&level->field, SPRITE_TEXTURED, 1920 / 2, 200 + (880 / 2), 1560, 880, 1, 0, field_image);

        }else
            if(contents[i]->c_str()[0] == 'b'){
                Bunker bunker;
                float x, y, scale, orientation, collider_type;
                char bunker_image[128];
                sscanf(contents[i]->c_str(), "b %f %f %f %f %f %s", &x, &y, &scale, &orientation, &collider_type, &bunker_image);
                dbg_output->Print("\tLoading Bunker: %s\n", bunker_image);
                renderer->CreateSprite(&bunker.sprite, SPRITE_TEXTURED | SPRITE_SIZE_FROM_IMAGE, x, y, (float)0, (float)0, scale, orientation * (3.14159 / 180), bunker_image);
                level->bunkers.push_back(bunker);
            }
        delete contents[i];
    }
    file.close();

    dbg_output->Print("Level Loaded!\n");
}

void WorldManager::ClearLevel(void)
{
    delete level;
    level = NULL;
}
